/**版本1.0,生成时间:2017-04-17 14:54:43**/
package com.xbdun.dao.bmp.provider;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * 动态XbdunRoleModel的SQL语句提供者
 * 角色
 * @author 张国涛
 * @2017-04-17 14:54:43
 */
public class RoleSqlProvider {

	//日志
	Log log = LogFactory.getLog(RoleSqlProvider.class);
	
	/**
	 * 动态sql更新对象
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updateDynXbdunRoleSql(Map<String,Object> map){
		
		StringBuilder sbSql = new StringBuilder();
		Map<String,Object> params = (Map<String, Object>) map.get("param");
		String _where = constWhere(map);
		boolean bl = false;
		
		Assert.isTrue(StringUtils.isNotEmpty(_where),"动态更新操作，条件Where不存在");
		
		//移除Where条件
		sbSql.append("UPDATE xbdun_role SET ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = params.entrySet().iterator();
			while(iparam.hasNext()){
				if(bl)sbSql.append(",");
				
				Entry<String, Object> enty = iparam.next();
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
			//设置Where条件
			if(!StringUtils.isEmpty(_where)){
				sbSql.append(" WHERE "+_where);
			}
		}else{
			//参数为空则，清空所有的SQL
			sbSql.setLength(0);
		}
		String sqlContent = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return sqlContent;
	}

	/**
	 * 查询分页
	 * @param params
	 * @return
	 */
	public String queryPagerSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//排序规则
		String orderBy = (String) params.get("_orderby");
		//当前页
		int pageIndex = (Integer) params.get("pageIndex");
		//显示条数
		int pageSize = (Integer) params.get("pageSize");
		//defSystem
		sbSql.append("SELECT * ");
		sbSql.append(" FROM xbdun_role WHERE 1=1 ");
		String where = constWhere(params);
		if(!StringUtils.isEmpty(where)){
			sbSql.append(" AND ");
			sbSql.append(where);
		}
		
		//查看排序
		if(!StringUtils.isEmpty(orderBy)){
			sbSql.append(" ORDER BY "+orderBy);
		}
		
		//分页
		if(pageSize>0){
			sbSql.append(" LIMIT "+pageIndex+","+pageSize);
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
	
	/**
	 * 查询分页查询总条数
	 * @param params
	 * @return
	 */
	public String queryPagerCountSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		
		sbSql.append("SELECT COUNT(*) ");
		sbSql.append(" From xbdun_role WHERE 1=1 ");
		String where = constWhere(params);
		if(!StringUtils.isEmpty(where)){
			sbSql.append(" AND ");
			sbSql.append(where);
		}
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
		
	}
	//生成查询分页where条件
	private String constWhere(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		//where查询条件
		@SuppressWarnings("unchecked")
		Map<String,Object> _where = (Map<String,Object>) params.get("_where");
		if(!CollectionUtils.isEmpty(_where)){
			Iterator<Entry<String, Object>> iparam = _where.entrySet().iterator();
			boolean bl = false;
			while(iparam.hasNext()){
				Entry<String, Object> enty = iparam.next();
				if(bl){sbSql.append(" AND ");}
				//判断是否为数组
				if(enty.getValue().getClass().isArray()){
					//判断是否ID
					if(enty.getKey().equals("notEqtId")){
						sbSql.append(" id NOT ");
					}else{
						sbSql.append(enty.getKey());
					}
					sbSql.append(" IN (");
					for(int i=0;i<((Object[])enty.getValue()).length;i++){
						if(i>0){sbSql.append(",");}
						sbSql.append("#{_where."+enty.getKey()+"["+i+"]}");
					}
					sbSql.append(")");
					if(!bl)bl = true;
					continue;
				}
				if(enty.getKey().equals("notEqt")){
					sbSql.append("#{_where."+enty.getKey()+"}");
					if(!bl)bl = true;
					continue;
				}
				sbSql.append(enty.getKey()+" = "+"#{_where."+enty.getKey()+"}");
				if(!bl)bl = true;
			}
		}
		
		String contentSql = sbSql.toString();
		
		sbSql.setLength(0);
		sbSql = null;
		
		return contentSql;
	}
}
